//
//  PainterTextAVG.m
//  ChartRefactor
//
//  Created by CaoJun on 15/3/25.
//  Copyright (c) 2015年 CaoJun. All rights reserved.
//

#import "PainterTextAVG.h"

using namespace ytx;
using namespace std;

@implementation PainterTextAVG

- (void) drawRightSideText:(CGRect)cgrect withContext:(ChartCanvas *) ctx withViewModelIndexPosition:(ViewModelIndexPosition *) indexPosition
{
    Quotation * data = indexPosition->getData();
    
    YTXConfig * config = YTXConfig::getInstance();
    ytx::Style style = config->style;
    Rectangle & rect = indexPosition->getRectangle();
    
    int startIndex = indexPosition->getStartDataIndex(), endIndex = indexPosition->getEndDataIndex();
    
    std::vector<std::pair<double, std::string> > leftSideTextList = data->getLeftSideText(startIndex, endIndex, rect, style);
    //    double baseValue = leftSideTextList[leftSideTextList.size() / 2].first;
    CGFloat DistanceY = rect.getDistanceY();
    CGPoint point = CGPointMake(rect.getEndX(), rect.getEndY());
    
    CGRect leftSideTextSize;
    
    double baseValue = leftSideTextList[leftSideTextList.size() / 2].first;
    
    for (vector<pair<double, string> >::const_iterator it = leftSideTextList.begin();
         it != leftSideTextList.end();
         ++it, point.y -= DistanceY)
    {
        double value = (*it).first;
        
        NSString * text;
        
        ChartCanvasTextAnchor anchor;
        
        if (it == leftSideTextList.begin()) {
            anchor = RightBottom;
        } else if (it + 1 == leftSideTextList.end()) {
            anchor = RightTop;
        } else {
            anchor = RightMiddle;
        }
        
        text = [NSString stringWithUTF8String: Tools::numberToFixedPercentString(fabs((value * 100 / baseValue) - 100)).c_str()];

        leftSideTextSize = [ctx fillText:text withX:point.x withY:point.y withColor:[NSString stringWithUTF8String:(*it).second.c_str()] withFont:[ NSString stringWithUTF8String:style.font.c_str() ] withSize:style.size withAnchor:anchor];
    }

}

- (void) drawBottomSideText:(CGRect)cgrect withContext:(ChartCanvas *) ctx withViewModelIndexPosition:(ViewModelIndexPosition *) indexPosition
{
    AVGData * data = (AVGData *)indexPosition->getData();
    
    YTXConfig * config = YTXConfig::getInstance();
    ytx::Style style = config->style;
    Rectangle & rect = indexPosition->getRectangle();
    
    NSString * font = [NSString stringWithUTF8String:style.font.c_str()];
    CGFloat size = style.timeRangeSize;
    NSString * color = [NSString stringWithUTF8String:style.timeRangeColor.c_str()];
    
    CGPoint point;
    
    const vector<string> & bottomSideTextList(data->getBottomSideText(rect.getWidthType()));
    
    if (bottomSideTextList.size() < 1) {
        return;
    }
    
    CGFloat DistanceX = rect.getDistanceX();
    
    point = CGPointMake(rect.getBeginX(), rect.getEndY());
    
    ChartCanvasTextAnchor alignAnchorPoint;
    
    for (vector<string>::const_iterator it = bottomSideTextList.begin(); it != bottomSideTextList.end(); ++it, point.x += DistanceX) {
        if (it == bottomSideTextList.begin()) {
            alignAnchorPoint = LeftTop;
        } else if (it + 1 == bottomSideTextList.end()) {
            alignAnchorPoint = RightTop;
        } else {
            alignAnchorPoint = CenterTop;
        }
        [ctx fillText:[NSString stringWithUTF8String:(*it).c_str()] withX:point.x withY:point.y withColor: color withFont:font withSize:size withAnchor:alignAnchorPoint];
    }
}


@end
